package com.grf.controller;

import com.grf.pojo.User;
import com.grf.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @HystrixCommand(fallbackMethod = "getUserHystrix")
    @ResponseBody
    @RequestMapping("/getUser")
    public User getUser() throws Exception {
        User user = userService.getUser();
        if (user.getId().equals(1)) {
            throw new Exception("发生异常");
        }
        return user;
    }

    //备选方案
    public User getUserHystrix() {
        User user = new User();
        user.setId(2);
        user.setName("Hystrix");
        return user;
    }

}
